﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;

using DTO;
using Model;

namespace DataService
{
    public class UserService : BaseService
    {
        private UserModel _userModel;
        private CustomerService _customerService;

        private const int ROLE = 1;
        private const int SERVER_ROLE = 2;

        public UserService () : base()
        {
            _userModel = new UserModel();
            _customerService = new CustomerService();
        }


        public IList<User> GetListUserByAccountId(int AccountId)
        {
            DataTable dt = _userModel.GetListUserByAccountId(AccountId);
            IList<User> users = new List<User>();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    User user = new User(BaseModel.GetIntFieldValue(dr, UserModel.USER_ID_FIELD));

                    user.Customer = new Customer(BaseModel.GetIntFieldValue(dr, "AccountId"))
                    {
                        Code=BaseModel.GetStringFieldValue(dr,"AccountName")
                    };
                    user.Username = BaseModel.GetStringFieldValue(dr,UserModel.USER_NAME_FIELD);
                    user.Role = new User.RoleInfo(BaseModel.GetIntFieldValue(dr, UserModel.ROLE_IS_ADMIN_FIELD))
                    {
                        Name = BaseModel.GetStringFieldValue(dr, UserModel.ROLE_ADMIN_NAME_FIELD)
                    };
                    user.FirstName = BaseModel.GetStringFieldValue(dr, UserModel.FIRST_NAME_FIELD);
                    user.LastName = BaseModel.GetStringFieldValue(dr, UserModel.LAST_NAME_FIELD);
                    user.Email = BaseModel.GetStringFieldValue(dr, UserModel.EMAIL_FIELD);
                    user.Phone = BaseModel.GetStringFieldValue(dr, UserModel.PHONE_FIELD);
                    user.IsAdmin = BaseModel.GetBooleanFieldValue(dr, UserModel.IS_ADMIN_FIELD);
                    user.IsActived = BaseModel.GetBooleanFieldValue(dr, UserModel.IS_ACTIVE_FIELD);
                    users.Add(user);
                }
            }
            return users;
        }

        public void ChangePasswordByUserName(string UserName, string PasswordNew)
        {
            _userModel.ChangePasswordByUserName( UserName, PasswordNew);
        }

        public void DeleteUserByUserId(int UserId)
        {
            _userModel.DeleteUserByUserId(UserId);
        }

        public void SetActiveUser(int UserId, bool IsActive)
        {
            _userModel.SetActiveUser(UserId, IsActive);
        }

        public int InsertUpdateUserInfo(int AccountId, int? UserId, string UserName, string Password, string FirstName, string LastName, string Email, string Phone)
        {
            return Convert.ToInt32(_userModel.InsertUpdateUserInfo(AccountId, UserId, UserName, Password, FirstName, LastName, Email, Phone, false));
        }

        public void InsertUpdateUserRoleAssociation(int UserId, int RoleId, bool IsAllow)
        {
            _userModel.InsertUpdateUserRoleAssociation(UserId, RoleId, IsAllow);
        }


        public User Login(string username, string password)
        {
            DataRow row = _userModel.GetInfoLoginByUserName(username, password);

            if (row != null)
            {                
                User user = new User(UserModel.GetIntFieldValue(row, "UserId"))
                {
                    FirstName = UserModel.GetStringFieldValue(row, "UserFirstName"),
                    LastName = UserModel.GetStringFieldValue(row, "UserLastName"),
                    Email = UserModel.GetStringFieldValue(row, "Email"),
                    Phone = UserModel.GetStringFieldValue(row, "Phone"),
                    IsActived = true,
                    Username = username,
                    Password = password
                };

                user.Customer = _customerService.GetCustomerInfo(user);

                int id = UserModel.GetIntFieldValue(row, "UserAddressId");
                if (id > 0)
                {
                    user.Location = (new ServiceHelper()).GetLocationById(id);
                }

                DataRow dr = _userModel.CheckIsAdminByUserId(user.Id);
                if(dr!=null)
                    user.IsAdmin = true;
                else 
                    user.IsAdmin = false;
                return user;
            }

            return null;
        }

        public IList<User.RoleInfo> GetRoles ( User login )
        {
            DataTable table = _userModel.GetListUserRoleAssociationByUserIdAndRoleTypeId(login.Id, ROLE);
            IList<User.RoleInfo> list = new List<User.RoleInfo>();

            if ( table != null )
            {
                DataView view = table.DefaultView;
                //view.RowFilter = "RoleId = 1";

                foreach ( DataRowView row in view )
                {
                    list.Add(new User.RoleInfo(BaseModel.GetIntFieldValue(row, "AutoId"))
                    {
                        Name = BaseModel.GetStringFieldValue(row, "AllowName"),
                        Description = BaseModel.GetStringFieldValue(row, "AllowNotes"),
                        IsAllow=BaseModel.GetBooleanFieldValue(row,"IsAllow")
                    });
                }
            }

            return list;
        }
        public IList<User.ServerRoleInfo> GetServerRoles ( User login )
        {
            DataTable table = _userModel.GetListUserRoleAssociationByUserIdAndRoleTypeId(login.Id, SERVER_ROLE);
            IList<User.ServerRoleInfo> list = new List<User.ServerRoleInfo>();

            if ( table != null )
            {
                DataView view = table.DefaultView;
                //view.RowFilter = "RoleId = 2";

                foreach ( DataRowView row in table.DefaultView )
                {
                    list.Add(new User.ServerRoleInfo(BaseModel.GetIntFieldValue(row, "AutoId"))
                    {
                        Name = BaseModel.GetStringFieldValue(row, "AllowName"),
                        Description = BaseModel.GetStringFieldValue(row, "AllowNotes"),
                        IsAllow = BaseModel.GetBooleanFieldValue(row, "IsAllow")
                    });
                }
            }

            return list;
        }
    }
}
